<?php

class OrdersModule extends fvModule {

    function __construct () {
        $moduleName = strtolower(substr(__CLASS__, 0, -6));

        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showDoc() {
    	$request = fvRequest::getInstance();
    	if (!$Order = OrderManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Order = new Order();
        }
    	$this->__assign(array(
        	'Products' => ProductsManager::getInstance(),
    		'today' => date('Y-m-d') ,
            'Order' => $Order,
        ));
		return $this->__display("order_doc_{$request->getEscapedParameter("type")}.tpl");
    }

    function showIndex() {
        if (!fvRequest::getInstance()->hasRequestParameter("page")) {
            if (fvSite::$fvSession->is_set(fvRequest::getInstance()->getRequestParameter("requestURL")."/page")) {
                fvRequest::getInstance()->putRequestParameter("page",
                    (int)fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/page"));
            } 
        } else {
            fvSite::$fvSession->set(fvRequest::getInstance()->getRequestParameter("requestURL")."/page", 
                fvRequest::getInstance()->getRequestParameter("page"));
        }

        if (substr($requestURL = fvRequest::getInstance()->getRequestParameter("requestURL"), 0, 1) == "/") {
            $requestURL = substr($requestURL, 1);
        }
        
   		$filter = fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/filter");
    	$sort = fvRequest::getInstance()->getRequestParameter("sort") == 'asc' ? 'desc' : 'asc';
    	$this->__assign('sort', $sort);
    	$params = array();
        if (is_array($filter)) {
            if (!empty($filter['date_from'])) {
                $query .= (($query)?" AND ":'') . "create_date >= ?";
                $params[] = $filter['date_from'];
                $this->__assign('filter_date_from', $filter['date_from']);
            }
            if (!empty($filter['date_to'])) {
                $query .= (($query)?" AND ":'') . "create_date <= ?";
                $params[] = $filter['date_to'];
                $this->__assign('filter_date_to', $filter['date_to']);
            }
        	if (!empty($filter['delivery'])) {
                $query .= (($query)?" AND ":'') . "delivery = ?";
                $params[] = $filter['delivery'];
                $this->__assign('filter_delivery', $filter['delivery']);
            }
        	if (!empty($filter['payment'])) {
                $query .= (($query)?" AND ":'') . "payment = ?";
                $params[] = $filter['payment'];
                $this->__assign('filter_payment', $filter['payment']);
            }
        	if (!empty($filter['status'])) {
                $query .= (($query)?" AND ":'') . "status = ?";
                $params[] = $filter['status'];
                $this->__assign('filter_status', $filter['status']);
            }
        	if (!empty($filter['status_exclude'])) {
                $query .= (($query)?" AND ":'') . "status <> ?";
                $params[] = $filter['status_exclude'];
                $this->__assign('filter_status_exclude', $filter['status_exclude']);
            }
        	if (!empty($filter['manager_id'])) {
                $query .= (($query)?" AND ":'') . "manager_id = ?";
                $params[] = $filter['manager_id'];
                $this->__assign('filter_manager_id', $filter['manager_id']);
            }
        }
        
        $pager = new fvPager(OrderManager::getInstance());
        $this->__assign('User', fvSite::$fvSession->getUser());
        $this->__assign('Orders', $pager->paginate($query, "create_date DESC", $params));
        $this->__assign('Products', ProductsManager::getInstance());
        $this->__assign('UserManager', UserManager::getInstance());
        return $this->__display('order_list.tpl');    
    }

    function showShow() {
        $request = fvRequest::getInstance();
        if (!$Order = OrderManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Order = new Order();
        }

        $this->__assign(array(
        	'Products' => ProductsManager::getInstance(),
            'Order' => $Order,
        	'options' => array(
        		//'unconfirmed' => 'Ожидает подтверждения', 
        		'inwork' => 'В работе',
        		'ready_for_delivery' => 'Готов к доставке', 
        		'delivered' => 'Доставлен',
        		'returned' => 'Возвращен',
        	),
        	'payment_options' => array(
        		'0' => 'Не оплачен',
        		'1' => 'Оплачен', 
        	),
        	'User' => fvSite::$fvSession->getUser(),
        ));
        
        return $this->__display('order_show.tpl');
        
    }
    
}

?>
